﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.BLL.Center;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Dto.Center.ProductBase;
using SHKF.B2C.Model.Dto.Center.ProductBaseImage;
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.Model.Req;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHKF.B2C.Mannage.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class ProductBaseController : BaseController
    {
        private readonly ProductBaseBLL productBaseBLL;
        private readonly ProductBaseImageBLL productBaseImageBLL;

        public ProductBaseController(ProductBaseBLL productBaseBLL,
            ProductBaseImageBLL productBaseImageBLL,
            ILogger<DataWarehousInfoController> logger) : base(logger)
        {
            this.productBaseBLL = productBaseBLL;
            this.productBaseImageBLL = productBaseImageBLL;
        }

        [HttpPost("List")]
        public ProductBasePageDto List(ProductBasePageReq req)
        {
            ProductBasePageDto dto = new ProductBasePageDto();

            try
            {
                int totalNumber = 0;
                    int totalPage = 0;

                dto.data = this.productBaseBLL.PageList(req,ref totalNumber, ref totalPage);
                dto.Page = new PageInfo() { Total = totalNumber, PageCount = totalPage, PageSize = req.PageSize, PageIndex = req.PageIndex };
            }
            catch (Exception ex)
            {
                dto.SetException(ex); 
            }
            return dto;
        }

        [HttpPost("Save")]
        public ProductBaseDto Save(ProductBaseEntity data)
        {
            ProductBaseDto dto = new ProductBaseDto();

            try
            {
                if (data.SN == 0 && this.productBaseBLL.IsExist(data.Upc))
                {
                    dto.error = new Error() { code = -1, msg = "此Upc产品已经存在,无法添加!" };
                    return dto;
                }
                dto.data = this.productBaseBLL.Save(data);
            }
            catch (Exception ex)
            {
                dto.SetException(ex);
            }
            return dto;
        }

        [HttpPost("Delete")]
        public DataDto Delete(List<long> snList)
        {
            DataDto dto = new DataDto();

            try
            {
                int result =this.productBaseBLL.Delete(snList);
            }
            catch (Exception ex)
            {
                dto.SetException(ex);
            }
            return dto;
        }

        [HttpGet("Image/List")]
        public ProductBaseImageListDto ImmageList(long FKSN)
        { 
            ProductBaseImageListDto dto = new ProductBaseImageListDto();
            try
            {
                List<ProductBaseImageEntity> data = this.productBaseImageBLL.Query(FKSN);
                dto.data = data;
            }
            catch (Exception ex)
            {
                dto.SetException(ex); 
            }

            return dto;
        }

        [HttpPost("Image/Add")]
        public ProductBaseImageDto Add(ProductBaseImageEntity data)
        {
            ProductBaseImageDto dto = new ProductBaseImageDto();
            try
            { 
                data = this.productBaseImageBLL.Add(data);
                dto.data = data;
            }
            catch (Exception ex)
            {
                dto.SetException(ex);
            }

            return dto;
        }

        [HttpPost("Image/Delete")]
        public DataDto Delete(ProductBaseImageEntity data)
        {
            DataDto dto = new DataDto();
            try
            {
                int result = this.productBaseImageBLL.Delete(data); 
            }
            catch (Exception ex)
            {
                dto.SetException(ex);
            }

            return dto;
        }

    }
}
